<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>生成一个随机令牌</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oauthprovider.consumerhandler.html">OAuthProvider::consumerHandler</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="oauthprovider.is2leggedendpoint.html">OAuthProvider::is2LeggedEndpoint</a></div>
 <div class="up"><a href="class.oauthprovider.html">OAuthProvider</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="oauthprovider.generatetoken" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">OAuthProvider::generateToken</h1>
  <p class="verinfo">(PECL OAuth &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">OAuthProvider::generateToken</span> &mdash; <span class="dc-title">生成一个随机令牌</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-oauthprovider.generatetoken-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">string</span> <span class="methodname"><strong>OAuthProvider::generateToken</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$size</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$strong</code><span class="initializer"> = false</span></span>
  ] )</div>

  <p class="para rdfs-comment">
  生成一个伪随机字节的 <span class="type"><span class="type 字符串">字符串</span></span> 。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-oauthprovider.generatetoken-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">size</code></em></dt>

    <dd>

     <p class="para">
     想要的令牌长度，单位为字节。
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">strong</code></em></dt>

    <dd>

     <p class="para">
     设置为 <strong><code>TRUE</code></strong> 则意味着将对熵使用 <em>/dev/random</em> ，否则使用非阻塞的 <em>/dev/urandom</em>。在 Windows 平台将忽略此参数。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-oauthprovider.generatetoken-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   生成的令牌，一个以字节为单位的 <span class="type"><span class="type 字符串">字符串</span></span> 。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-oauthprovider.generatetoken-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
如果 <em><code class="parameter">strong</code></em> 参数为 <strong><code>TRUE</code></strong> ， 则当回退到用 <span class="function"><a href="function.rand.html" class="function">rand()</a></span> 来实现填充剩余的随机字节的时候，将触发一个 <strong><code>E_WARNING</code></strong> 级别的错误（比如，当最初找不到足够的随机数据的时候）。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-oauthprovider.generatetoken-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-5174">
   <p><strong>Example #1 <span class="function"><strong>OAuthProvider::generateToken()</strong></span> 例子</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$p&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">OAuthProvider</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">generateToken</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$t</span><span style="color: #007700">),&nbsp;&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$t</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
4
b6a82c27
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-oauthprovider.generatetoken-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    当系统没有足够的随机数据可用的时候，此函数将使用 PHP 内部的 <span class="function"><a href="function.rand.html" class="function">rand()</a></span> 来实现填充剩余的随机字节。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-oauthprovider.generatetoken-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.openssl-random-pseudo-bytes.html" class="function" rel="rdfs-seeAlso">openssl_random_pseudo_bytes()</a> - Generate a pseudo-random string of bytes</span></li>
   <li class="member"><span class="function"><a href="function.mcrypt-create-iv.html" class="function" rel="rdfs-seeAlso">mcrypt_create_iv()</a> - Creates an initialization vector (IV) from a random source</span></li>
  </ul>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oauthprovider.consumerhandler.html">OAuthProvider::consumerHandler</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="oauthprovider.is2leggedendpoint.html">OAuthProvider::is2LeggedEndpoint</a></div>
 <div class="up"><a href="class.oauthprovider.html">OAuthProvider</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
